{-------------------------------------------------------------------------------
  Author: Duo (iamduo@qq.com)
  DateTime: 2019/7/13 21:20:36

  DB.TParams
-------------------------------------------------------------------------------}
unit uDBParam_Params;

interface

uses uDB_Base, uBizDBIntf, SysUtils, DB, Variants;

type
  TDBParam_Param = class(TDBLockedBase, IDBParam)
  private
    FParam:TParam;
  public
    constructor Create(const ADBParamsIntf:IDBParams; const AParam:TParam);
    destructor Destroy;override;
    { IDBParam }
    function IsBlob:Boolean;
    function IsNull:Boolean;
    function Name:WideString;
    function Kind:WideString;
    function Size:Integer;
    function GetValue:OleVariant;
    procedure SetValue(const AValue:OleVariant);
    property Value:OleVariant read GetValue write SetValue;
  end;

  TDBParam_Params = class(TDBLockedBase, IDBParams)
  private
    FParams:TParams;
  public
    constructor Create(const ADBDataIntf:IDBData; const AParams:TParams);
    destructor Destroy;override;
    { IDBParams }
    function Count:Integer;
    function Add:Integer;
    function Delete(const AIndex:Integer):Boolean;
    function GetParams(const AIndex:Integer):IDBParam;
    procedure SetParams(const AIndex:Integer; const AValue:IDBParam);
    property Params[const AIndex:Integer]:IDBParam read GetParams write SetParams;default;
  end;

implementation

uses Classes;

{ TDBParam_Param }

constructor TDBParam_Param.Create(const ADBParamsIntf: IDBParams;
  const AParam: TParam);
begin
  inherited Create(ADBParamsIntf);
  FParam := AParam;
end;

destructor TDBParam_Param.Destroy;
begin
  FParam := nil;
  inherited;
end;

function TDBParam_Param.GetValue: OleVariant;
begin
  Result := FParam.Value;
end;

function TDBParam_Param.IsBlob: Boolean;
begin
  Result := FieldType_IsBlob(FParam.DataType);
end;

function TDBParam_Param.IsNull: Boolean;
begin
  Result := VarIsNull(FParam.Value);
end;

function TDBParam_Param.Kind: WideString;
begin
  Result := FieldType_ToString(FParam.DataType);
end;

function TDBParam_Param.Name: WideString;
begin
  Result := FParam.Name;
end;

procedure TDBParam_Param.SetValue(const AValue: OleVariant);
begin
  FParam.Value := AValue;
end;

function TDBParam_Param.Size: Integer;
begin
  Result := FParam.Size;
end;

{ TDBParam_Params }

function TDBParam_Params.Add: Integer;
begin
  Lock;
  try
    Result := FParams.Count;
    FParams.Add;
  finally
    UnLock;
  end;
end;

function TDBParam_Params.Count: Integer;
begin
  Lock;
  try
    Result := FParams.Count;
  finally
    UnLock;
  end;
end;

constructor TDBParam_Params.Create(const ADBDataIntf: IDBData;
  const AParams: TParams);
begin
  inherited Create(ADBDataIntf);
  FParams := AParams;
end;

function TDBParam_Params.Delete(const AIndex: Integer): Boolean;
begin
  Lock;
  try
    Result := false;
    if (AIndex >= 0) and (AIndex < FParams.Count) then
    begin
      FParams.Delete(AIndex);
      Result := true;
    end;
  finally
    UnLock;
  end;
end;

destructor TDBParam_Params.Destroy;
begin
  FParams := nil;
  inherited;
end;

function TDBParam_Params.GetParams(const AIndex: Integer): IDBParam;
begin
  Lock;
  try
    Result := TDBParam_Param.Create(Self, FParams[AIndex]);
  finally
    UnLock;
  end;
end;

procedure TDBParam_Params.SetParams(const AIndex: Integer;
  const AValue: IDBParam);
begin
  Lock;
  try
    FParams[AIndex].Name := AValue.Name;
    FParams[AIndex].DataType := FieldType_FromString(AValue.Kind);
    FParams[AIndex].Size := AValue.Size;
    FParams[AIndex].Value := AValue.Value;
  finally
    UnLock;
  end;
end;

end.

